package com.example.arithmeticleetcode.learnArithmetic2.dynamic_programming;

/**
 * @program: arithmetic-leetcode
 * @description: 最长公共子串
 * @author: FangZhen
 * @create: 2020-12-23 14:13
 **/
public class LCSubstring {

    public static void main(String[] args) {
        System.out.println(lcs("ABCD", "BABC"));
        System.out.println(lcs2("ABCD", "BABC"));
        System.out.println(lcs3("ABCD", "BABC"));
    }

    /**
     * 使用动态规划解决
     *
     * @param str1
     * @param str2
     * @return
     */
    public static int lcs3(String str1, String str2) {
        if (str1 == null || str2 == null) return 0;
        char[] chars1 = str1.toCharArray();
        if (chars1.length == 0) return 0;
        char[] chars2 = str2.toCharArray();
        if (chars2.length == 0) return 0;
        char[] cols = chars1, rows = chars2;
        if (chars1.length > chars2.length) {
            cols = chars2;
            rows = chars1;
        }
        int[] dp = new int[cols.length + 1];
        int max = 0;
        for (int row = 1; row <= rows.length; row++) {
            for (int col = cols.length; col >= 1; col--) {
                if (rows[row - 1] != cols[col - 1]) {
                    dp[col] = 0;
                } else {
                    dp[col] = dp[col - 1] + 1;
                    max = Math.max(max, dp[col]);
                }
            }
        }
        return max;
    }


    /**
     * 时间复杂度 O(k*m) 时间复杂度为O(min(k,m))
     *
     * @param str1
     * @param str2
     * @return
     */
    public static int lcs2(String str1, String str2) {
        if (str1 == null || str2 == null) return 0;
        char[] chars1 = str1.toCharArray();
        if (chars1.length == 0) return 0;
        char[] chars2 = str2.toCharArray();
        if (chars2.length == 0) return 0;
        char[] cols = chars1, rows = chars2;
        if (chars1.length > chars2.length) {
            cols = chars2;
            rows = chars1;
        }
        int[] dp = new int[cols.length + 1];
        int max = 0;
        for (int row = 1; row <= rows.length; row++) {
            int cur = 0;
            for (int col = 1; col <= cols.length; col++) {
                int leftTop = cur;
                cur = dp[col];
                if (rows[row - 1] != cols[col - 1]) {
                    dp[col] = 0;
                } else {
                    dp[col] = leftTop + 1;
                    max = Math.max(max, dp[col]);
                }
            }
        }
        return max;
    }

    /**
     * 空间复杂度与时间复杂度均为O(N^2)
     *
     * @param str1
     * @param str2
     * @return
     */
    public static int lcs(String str1, String str2) {
        if (str1 == null || str2 == null) return 0;
        char[] chars1 = str1.toCharArray();
        if (chars1.length == 0) return 0;
        char[] chars2 = str2.toCharArray();
        if (chars2.length == 0) return 0;
        int[][] dp = new int[chars1.length + 1][chars2.length + 1];
        int max = 0;
        for (int i = 1; i <= chars1.length; i++) {
            for (int j = 1; j <= chars2.length; j++) {
                if (chars1[i - 1] != chars2[j - 1]) continue;
                dp[i][j] = dp[i - 1][j - 1] + 1;
                max = Math.max(max, dp[i][j]);
            }
        }
        return max;
    }
}
